package cn.sh.yhk.configure.exception;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

//@ControllerAdvice
public class ExceptionHandlerConfiguration {
    /**
     * 所有异常报错
     *
     * @param request
     * @param exception
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Map<String, Object> allExceptionHandler(HttpServletRequest request,
                                                   Exception exception) throws Exception {
        Map<String, Object> map = new HashMap<>();
        System.out.println(exception.toString());
        StringBuffer stack = new StringBuffer();
        for (StackTraceElement el :exception.getStackTrace()){
            stack.append(el.toString()+"\n");
        }
        System.out.println(stack);
       // map.put("exception", null != exception.getCause() ? exception.getCause() : exception);
        map.put("errorMessage", stack);
        map.put("url", request.getRequestURL());
        map.put("statusCode", 500);
        return map;
    }

}
